package com.laijiale.mapper;

import com.laijiale.entities.Assets;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 11:51
 */
@Repository
public interface AssetsMapper {
    @Insert("insert into rms_assets (title,picture,price,price_unit,stock,stock_unit,total,info,deleted,created,updated )" +
            "values(#{title},#{picture},#{price},#{priceUnit},#{stock},#{stockUnit},#{total},#{info},#{deleted},#{created},#{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Assets assets);


    @Select("select ra.id, ra.title,ra.picture,ra.price,ra.price_unit,ra.stock_unit,ra.stock,ra.total,ra.info, ra.deleted, ra.created, ra.updated \n" +
            "from rms_assets ra" +
            " where ra.id=#{param1} and ra.deleted=0")
    Assets selectById(Long id);

    @Update("<script>" +
            "update rms_assets" +
            "<set>" +
            "<if test='title !=null'>title=#{title},</if> " +
            "<if test='picture !=null'>picture=#{picture},</if> " +
            "<if test='price !=null'>price=#{price},</if> " +
            "<if test='priceUnit !=null'>price_unit=#{priceUnit},</if> " +
            "<if test='stock !=null'>stock=#{stock},</if> " +
            "<if test='stockUnit !=null'>stock_unit=#{stockUnit},</if> " +
            "<if test='total !=null'>total=#{total},</if> " +
            "<if test='info !=null'>info=#{info},</if> " +
            "<if test='deleted !=null'>deleted=#{deleted},</if> " +
            "<if test='created !=null'>created=#{created},</if> " +
            "<if test='updated !=null'>updated=#{updated},</if> " +
            "</set> " +
            "where id =#{id}  and deleted =0" +
            "</script>")
    int updateById(Assets assets);

    @Update("update rms_assets set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);
    @Update("<script>" +
            "update rms_assets set deleted =1,updated = now() where " +
            "<foreach collection = 'list' item='e' open ='id in (' close= ')' separator = ','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Select("select  ra.id, ra.title,ra.picture,ra.price,ra.price_unit,ra.stock,ra.stock_unit, ra.total,ra.info, ra.deleted, " +
            "ra.created,ra.updated  from rms_assets ra ")
    List<Assets> list();

    @Select("select ra.id, ra.title from rms_assets ra " +
            "where ra.title = #{title} ")
     Assets selectByAllTitle(Assets assets);

    @Delete("delete from rms_assets where id =#{param1}")
    int deleteById(Long id);

    @Select("select ra.id, ra.title from rms_assets ra " +
            "where ra.title = #{param1} ")
    Assets selectByTitle(String title);


    @Select("select  ra.id, ra.title, ra.picture, ra.price,ra.price_unit, ra.stock, ra.stock_unit,ra.total, ra.info,ra.deleted, " +
            "ra.created,ra.updated  from rms_assets ra "+
            "where ra.deleted=0 and ra.title like concat('%',#{param1},'%') "
    )
    List<Assets> listLikeTitle(String title);



}
